package com.tungse.juc.volatile_test;

import javax.sound.midi.Soundbank;
import java.util.concurrent.TimeUnit;

/**
 * @author Tungse
 */
public class JMMDemo {

    //未添加volatile关键字，线程无法感知变量的修改, 如下方法不会结束，因为主线程修改了num的值，但是线程无法感知
    private static volatile int num = 0;
    public static void main(String[] args) throws InterruptedException {
        new Thread(()->{
            while (num == 0) {

            }
        }).start();
        TimeUnit.SECONDS.sleep(1);

        num = 1;
        System.out.println("main over; num = " + num);
    }
}
